package com.lgmshare.k3.ui.base;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;

import com.lgmshare.component.app.LaraFragment;
import com.lgmshare.component.utils.UIUtils;
import com.lgmshare.k3.ui.dialog.CommonDialog;
import com.lgmshare.k3.ui.dialog.LoadingDialog;
import com.lgmshare.k3.utils.AnalyticsUtils;

import butterknife.ButterKnife;

/**
 * @author lim
 * @description: TODO
 * @email lgmshare@gmail.com
 * @datetime 2016/6/23 14:27
 */
public abstract class BaseFragment extends LaraFragment {

    /**
     * 加载等待框
     */
    private LoadingDialog mLoadingDialog;
    /**
     * Fragment动作监听
     */
    protected OnFragmentMessageListener mOnFragmentMessageListener;

    @TargetApi(23)
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        onAttachToContext(context);
    }

    /*
     * Deprecated on API 23
     * Use onAttachToContext instead
     */
    @SuppressWarnings("deprecation")
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            onAttachToContext(activity);
        }
    }

    /*
     * Called when the fragment attaches to the context
     */
    protected void onAttachToContext(Context context) {
        //do something
        if (context instanceof OnFragmentMessageListener) {
            mOnFragmentMessageListener = (OnFragmentMessageListener) context;
        }
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this, mRootView);
    }

    @Override
    public void onStart() {
        super.onStart();
        AnalyticsUtils.onPageStart(TAG);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onStop() {
        dismissProgressDialog();
        super.onStop();
        AnalyticsUtils.onPageEnd(TAG);
    }

    @Override
    public void onDestroyView() {
        dismissProgressDialog();
        super.onDestroyView();
    }

    /**
     * 提示信息
     *
     * @param res
     */
    protected void showToast(int res) {
        showToast(getString(res));
    }

    /**
     * 提示信息
     *
     * @param msg
     */
    protected void showToast(String msg) {
        if (!isDetached() && !TextUtils.isEmpty(msg)) {
            UIUtils.showToast(msg);
        }
    }

    /**
     * dialog提示信息
     *
     * @param res
     */
    protected void showDialogToast(int res) {
        showDialogToast(getString(res));
    }

    /**
     * dialog提示信息
     *
     * @param msg
     */
    protected void showDialogToast(String msg) {
        CommonDialog dialog = new CommonDialog(getActivity());
        dialog.setContent(msg);
        dialog.show();
    }

    /**
     * 显示加载框
     */
    protected void showProgressDialog() {
        showProgressDialog("");
    }

    /**
     * 显示加载框
     *
     * @param message
     */
    protected void showProgressDialog(CharSequence message) {
        showProgressDialog(message, true);
    }

    /**
     * 显示加载框
     *
     * @param message
     * @param cancelable 是否可以取消
     */
    protected void showProgressDialog(CharSequence message, boolean cancelable) {
        if (getActivity() != null && !getActivity().isFinishing()) {
            if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
                return;
            }
            mLoadingDialog = new LoadingDialog(getActivity());
            mLoadingDialog.setMessage(message);
            mLoadingDialog.setCancelable(cancelable);
            mLoadingDialog.setCanceledOnTouchOutside(cancelable);
            mLoadingDialog.show();
        }
    }

    /**
     * 隐藏加载等待框
     */
    public void dismissProgressDialog() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
            mLoadingDialog = null;
        }
    }

    /**
     * Activity与Fragment传递数据
     */
    public interface OnFragmentMessageListener {

        void onFragmentSendMessage(int position, String message);

        void onFragmentChange(int oldPosition, int newPosition);
    }
}
